﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCFrameWork.Web.Cacheing
{
    public abstract class CacheProviderBase : ICacheProvider
    {

        #region ICacheProvider Members

        public abstract void Set(string key, object value);

        public abstract void Set(string key, object value, DateTime expiration);

        public abstract void Remove(string key);

        public abstract bool Contains(string key);

        public abstract int Count { get; }

        public abstract void Flush();

        public abstract object GetData(string key);

        public virtual Dictionary<string, object> GetDatas(string[] keys)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            for (int i = 0; i < keys.Length; i++)
            {
                object data = this.GetData(keys[i]);
                if (data != null)
                {
                    result[keys[i]] = data;
                }
            }

            return result;
        }

        public virtual object[] GetDataArray(string[] keys)
        {
            object[] result = new object[keys.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                result[i] = this.GetData(keys[i]);
            }

            return result;
        }

        public virtual bool GetData<T>(string key, out T data)
        {
            object objData = this.GetData(key);
            data = objData == null ? default(T) : (T)objData;

            return objData != null;
        }

        #endregion
    }
}
